#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2014-08-22 01:14:23
# @Author  : shitao.tommy (hero007asd@gmail.com)
# @Link    : http://example.org
# @Version : $Id$

from framework.db.basedb import BaseDb
from tornado import gen
from lib import log,libtime


class Like(BaseDb):

    @gen.coroutine
    def ranklist(self, time_type=1, page=0, offset=20):
        '''点赞排行榜'''
        #TODO limit这样有问题，这昂数据会少很多
        resp = ''
        sel_sql = '''SELECT a.Fid, a.Femployee_id,count(a.Fid) num,a.Fcreate_time,b.Fname,b.Fimg,b.Fage,c.Fdorm_name
                        FROM tp_dorm_like a
                        LEFT JOIN tp_dorm_employee b
                        ON a.Femployee_id = b.Fid
                        LEFT JOIN tp_dorm_base c
                        ON b.Fdorm_id = c.Fid
                        WHERE a.Fcreate_time > %s AND a.Fis_like=1
                        GROUP BY a.Femployee_id
                        ORDER BY num DESC
                        LIMIT %s,%s'''
        start_time = 0
        if time_type == 1:
            start_time = libtime.get_day_start(0)
        elif time_type == 2:
            start_time = libtime.get_day_start(7)
        elif time_type == 3:
            start_time = libtime.get_day_start(30)
        try:
            ret = yield self.execute(sel_sql,(start_time,int(page), int(offset)))
            resp = []
            for i in ret:
                resp.append({'count':i[2],'empName':i[4],'img':i[5],'age':i[6],'dormName':i[7]})
        except Exception, e:
            import traceback
            e = traceback.format_exc()
            log.log_exception(self.__class__.__name__, e)
            resp = '-1'
        raise gen.Return(resp)


    @gen.coroutine
    def like(self, student_id,emp_id,is_like=1):
        '''点赞'''
        resp = ''
        sel_sql = '''SELECT Fid,Fis_like FROM tp_dorm_like WHERE Fstudent_id = %s AND Femployee_id = %s'''
        ins_sql = '''INSERT INTO tp_dorm_like(Fstudent_id,Femployee_id,Fis_like,Fcreate_time)
                    VALUES(%s,%s,%s,UNIX_TIMESTAMP())'''
        upd_sql = '''UPDATE tp_dorm_like SET Fis_like = 1 WHERE Fstudent_id = %s AND Femployee_id = %s AND Fis_like = 0'''
        try:
            sel_ret = yield self.execute(sel_sql,(student_id,emp_id))
            like_count = 0
            if len(sel_ret) > 0:
                for i in sel_ret:
                    if i[1] > 0:
                        like_count = like_count + 1
                if like_count > 1:
                    resp = '2' #已经点过赞
                else: #数据存在，但是是取消赞的，现在更新
                    ret = yield self.upd_or_ins(upd_sql,(student_id,emp_id))
                    if ret > 0:
                        resp = '1'
                    else: resp = '0'
            else:#数据不存在，点赞
                ret = yield self.upd_or_ins(ins_sql,(student_id,emp_id, is_like))
                if ret > 0:
                    resp = '1'
                else: resp = '0'
        except Exception, e:
            import traceback
            e = traceback.format_exc()
            log.log_exception(self.__class__.__name__, e)
            resp = '-1'
        raise gen.Return(resp)



    @gen.coroutine
    def cancel_like(self, student_id,emp_id):
        '''取消点赞'''
        resp = ''
        upd_sql = '''UPDATE tp_dorm_like SET Fis_like = 0 WHERE Fstudent_id = %s AND Femployee_id = %s AND Fis_like = 1'''
        try:
            ret = yield self.upd_or_ins(upd_sql,(student_id,emp_id))
            if ret > 0:
                resp = '1'
            else: resp = '0'
        except Exception, e:
            import traceback
            e = traceback.format_exc()
            log.log_exception(self.__class__.__name__, e)
            resp = '-1'
        raise gen.Return(resp)


